class MyPageHeader extends BaseEditMaster {
    constructor(dataId, currentTaskPrivileges, nextTaskPrivileges, pageStatus, pageListData,
            returnUrl, fetchDataUrl, saveUrl, exportUrl, printUrl) {
        super(dataId, currentTaskPrivileges, nextTaskPrivileges, pageStatus, pageListData,
                returnUrl, fetchDataUrl, saveUrl, exportUrl, printUrl);
    }
    // 得到数据
    GetEntityData() {
        var item = {};
        item['supplierId'] = $("#SupplierId").val();
        item['supplierNo'] = $.trim($("#SupplierNo").val());
        item['supplierName'] = $.trim($("#SupplierName").val());
        item['shortName'] = parent.formatNullInputValue($.trim($("#ShortName").val()));
        item['country'] = parent.formatNullInputValue($.trim($("#Country").val()));
        item['province'] = parent.formatNullInputValue($.trim($("#Province").val()));
        item['attn'] = parent.formatNullInputValue($.trim($("#Attn").val()));
        item['attnJob'] = parent.formatNullInputValue($.trim($("#AttnJob").val()));
        item['attnTel'] = parent.formatNullInputValue($.trim($("#AttnTel").val()));
        item['attnMobil'] = parent.formatNullInputValue($.trim($("#AttnMobil").val()));
        item['fax'] = parent.formatNullInputValue($.trim($("#Fax").val()));
        item['email'] = parent.formatNullInputValue($.trim($("#Email").val()));
        item['openAccountBank'] = parent.formatNullInputValue($.trim($("#OpenAccountBank").val()));
        item['bankAccount'] = parent.formatNullInputValue($.trim($("#BankAccount").val()));
        item['address'] = parent.formatNullInputValue($.trim($("#Address").val()));
        item['taxId'] = parent.formatNullInputValue($.trim($("#TaxId").val()));
        item['taxRateId'] = parent.formatNullInputValue($.trim($("#TaxRateId").val()));
        item['settlementModeId'] = parent.formatNullInputValue($.trim($("#SettlementModeId").val()));
        item['catalogueId'] = $.trim($("#CatalogueId").val());
        item['description'] = parent.formatNullInputValue($.trim($("#Description").val()));
        item['updatedBy'] = $("#UserId").val();

        return JSON.stringify(item);
    }
    FillData(data) {
        $("#SupplierId").val(data.supplierId);
        $("#SupplierNo").val(data.supplierNo);
        $("#SupplierName").val(data.supplierName);
        $("#ShortName").val(data.shortName);
        $("#Country").val(data.country);
        $("#Province").val(data.province);
        $("#Attn").val(data.attn);
        $("#AttnJob").val(data.attnJob);
        $("#AttnTel").val(data.attnTel);
        $("#AttnMobil").val(data.attnMobil);
        $("#Fax").val(data.fax);
        $("#Email").val(data.email);
        $("#OpenAccountBank").val(data.openAccountBank);
        $("#BankAccount").val(data.bankAccount);
        $("#Address").val(data.address);
        $("#TaxId").val(data.taxId);
        $("#TaxRateId").val(data.taxRateId);
        $("#TaxRateName").val(data.taxRateName);
        $("#SettlementModeId").val(data.settlementModeId);
        $("#SettlementModeName").val(data.settlementModeName);
        $("#CatalogueId").val(data.catalogueId);
        $("#CatalogueName").val(data.catalogueName);
        $("#Description").val(data.description);
        $("#NodeText").val(data.catalogueName);

        // 最后的审核人、审核日期
        //$("#CheckedBy").val(item.checkedBy);
        //$("#CheckedByName").val(item.checkedByName);
        //$("#CheckedDate").val(item.checkedDate === null ? '' : item.checkedDate);
        //$("#CheckedStatus").val(item.checkedStatus);
        //$('#FlowOrderNo').val(item.flowOrderNo);
        this.currentTaskPrivileges = data.currentTaskPrivileges;
        this.nextTaskPrivileges = data.nextTaskPrivileges;
        this.unCheckFlag = data.unCheckFlag;

        this.SetPageElementStatus();
        $("#SupplierNo").focus();
    }
    SetPageElementStatus() {
        super.SetPageElementStatus(function () {
            //$("#ButSave").hide();
        }, function () {
            //$("#ButSave").show();
        }, function () {

        });
    }
    Save(flag) {
        super.Save(function (data, $self) {
            if (data > 0) {
                if (parseInt($self.pageStatus) === 1) {
                    $("#SupplierId").val(data);   // 设置刚才插入的记录ID
                    $self.dataId = data;
                    $self.pageStatus = 2;
                }
                if (flag === 0) {
                    $self.New();
                }
            }
        });
    }
    OnNew() {
        // 默认设置一些值
        $('#SupplierId').val("0");
        super.OnNew();
        // 初始化所属材料分类
        $('#CatalogueId').val($("#NodeId").val());
        $('#CatalogueName').val($("#NodeText").val());
        $("#SupplierNo").focus();
    }
    DoChoiceTaxRate() {
        let _choice = new CommonChoice();
        _choice.DoChoiceTaxRate(0, (data) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#TaxRateName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#TaxRateId").val(data[0]);
                $("#TaxRateName").val(data[2]);
                $("#TaxRateName").focus();
            }
        });
    }
    DoChoiceSettlementMode() {
        let _choice = new CommonChoice();
        _choice.DoChoiceSettlementMode(0, (data) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#SettlementModeName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#SettlementModeId").val(data[0]);
                $("#SettlementModeName").val(data[1]);
                $("#SettlementModeName").focus();
            }
        });
    }
    DoChoiceCatalogue() {
        let _choice = new CommonChoice();
        _choice.DoChoiceCatalogue('/master/supplier/catalogue/choice.html', '选择所属供应商目录', (data) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#CatalogueName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#CatalogueId").val(data[0]);
                $("#CatalogueName").val(data[1]);
                $("#CatalogueName").focus();
            }
        });
    }
}

var _InstancePageHeader = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    let _pageStatus = $("#Action").val();
    let _dataId = $("#SupplierId").val();

    let supplier_list_result = $.cookie("supplier_list_result") === undefined ? "" : $.cookie("supplier_list_result");
    let _pageListData = [];
    if (supplier_list_result !== "") {
        _pageListData = supplier_list_result.split(',');
        $("#rec_total_rows").text(_pageListData.length);
        $("#rec_cur_row").text($.inArray(_dataId, _pageListData) + 1);

        if (_pageListData.length > 0) {
            let index = $.inArray(_dataId, _pageListData);
            if (index === 0) {
                $("#ButPrev").attr("disabled", "disabled");
            } else {
                $("#ButNext").removeAttr("disabled");
            }
            if (index === (_pageListData.length - 1)) {
                $("#ButNext").attr("disabled", "disabled");
            } else {
                $("#ButPrev").removeAttr("disabled");
            }
        }
    } else {
        $("#rec_total_rows").text("1");
        $("#rec_cur_row").text("1");
    }

    _InstancePageHeader = new MyPageHeader(_dataId, 0, 0, _pageStatus, _pageListData,
            "/master/supplier/list.html",
            "/master/supplier/fetchByKey.html",
            "/master/supplier/save.html",
            "/master/supplier/export.html",
            "/master/supplier/print.html");

    _InstancePageHeader.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    var w = $(window).width();
    var h = $(window).height();

    $(".data__table_wrapper").css({width: w - 40, height: h - 120});
}

//-----------------------------------------------------------------------------
// 验证函数
//-----------------------------------------------------------------------------
$.validator.setDefaults({
    ignore: 'hidden, [reaonly=readonly]'
});
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
    return arg !== value;
}, "Value must not equal arg.");

$(function () {
    $("#form_data").validate({
        // Step 1：设置验证规则和错误消息
        rules: {
            _SupplierNo: "required",
            _SupplierName: "required",
            _ShortName: "required",
            _Fax: "required",
            _TaxRateName: "required",
            _SettlementModeName: "required"
        },
        messages: {
            _SupplierNo: "请填写供应商编号。",
            _SupplierName: "请填写供应商名称。",
            _ShortName: "请填写供应商简称。",
            _Fax: "请填写传真。",
            _TaxRateName: "请填写传真。",
            _SettlementModeName: "请填写结算方式。"
        },
        // Step 2：设置验证触发事件
        onclick: false,
        onfocusout: false,
        onfocusin: false,
        onkeyup: false,
        onkeydown: false,
        focusInvalid: false,
        // Step 3：设置错误信息提示
        showErrors: window.showErrors  //使用自定义的错误显示方法
    });
});
